/**
 * Created by wangguoqing on 2017/12/28.
 */
var shStatus = new Array("停用", "启用");
var params = new Object();
var url = path + "/system/role/listByPage";
var roleId; //角色id

$(function () {
    //设置AjaxLoading
    setAjaxLoading();
    var grid = $("#tt");
    initGrid(grid);
    $("#divNorth").prev().find(".layout-button-up").hide();

    $("#tt").datagrid({
        //双击事件
        onDblClickRow: function (index, row) {
            btnQxfp();
        }
    });
});

//初始化表格和数据
function initGrid(grid) {
    var columns = [
        {
            field: "roleId",
            title: "ID",
            hidden: true,
            align: "left",
            sortable: false,
            resizable: true,
            width: 60
        },
        {
            field: "roleCode",
            title: "角色代码",
            align: "center",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 80
        },
        {
            field: "roleName",
            title: "角色名称",
            align: "center",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 150
        },
        {
            field: "description",
            title: "角色描述",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 200
        },
        {
            field: "mutexList",
            title: "互斥角色",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 250,
            formatter: function (value, row, index) {
                var js = "";
                $.each(row.mutexList, function (index, val) {
                    if (index == 0) {
                        js += val.roleName;
                    } else {
                        js += "，" + val.roleName;
                    }
                });
                return js;
            }
        },
        {
            field: "validMark",
            title: "状态",
            align: "center",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 60,
            formatter: function (value, row, index) {
                return shStatus[value];
            }
        },
        {
            field: "createUser",
            title: "创建人",
            align: "center",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 80
        },
        {
            field: "createTimeStr",
            title: "创建时间",
            align: "center",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 150
        }
    ];

    initPageGrid(grid, params, url, columns, "#tb");
    getPage(grid, params, url);
}

//查询按钮
function btnQuery() {
    params.roleCode = $("#roleCode").val();
    params.roleName = $("#roleName").val();
    params.delMark = '0';
    var grid = $("#tt");
    getPage(grid, params, url);
}

//重置按钮
function btnReset() {
    $("#roleCode").val('');
    $("#roleName").val('');
    var grid = $("#tt");
    params.roleCode = $("#roleCode").val();
    params.roleName = $("#roleName").val();
    params.delMark = '0';
    getPage(grid, params, url);
}

//增加
function btnAddNode() {
    layer.open({
        type: 2,
        title: ['系统角色 - 添加','background: #38acff;'],
        shadeClose: false,
        shade: 0.3,
        area: ['440px', '350px'],
        content: path+'/system/role/toAdd' //iframe的url
    });
}

//修改
function btnEditNodeByGrid() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        $.messager.alert("操作提示", "请选择一个角色信息！", "warning");
        //$.alert("pick", "info", "角色信息");
        return;
    }
    layer.open({
        type: 2,
        title: ['系统角色 - 修改','background: #38acff;'],
        shadeClose: true,
        shade: 0.3,
        area: ['440px', '350px'],
        content: path+'/system/role/toUpdate' //iframe的url
    });

}

//删除
function btnDelNodeByGrid() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        $.messager.alert("操作提示", "请选择一个角色信息！", "warning");
        //$.alert("pick", "info", "角色信息");
        return;
    }
    var rowData = rows[0];
    var param = new Object();
    param.roleId = rowData.roleId;

    $.messager.alert("问题", "请确认是否要删除？","confirm", function () {

            $.ajax({
                type: "POST",
                url: path + '/system/role/delete',
                data: param,
                dataType: "json",
                async: false,
                success: function callback(data) {
                    var analyMsg = data;
                    if (analyMsg.code != "0000") {
                        $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                        });
                        //$.alert("default", "info", analyMsg.msg);
                    } else {
                        $.messager.alert('操作提示', "删除成功！", 'info', function () {
                        });
                        btnQuery();
                    }
                }
            });

    });
}
//权限分配
function btnQxfp() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        $.messager.alert("操作提示", "请选择一个角色信息！", "warning");
        //$.alert("pick", "info", "角色信息");
        return;
    }
    var rowData = rows[0];
    var cdmk = new refTree('cdmk', '系统功能菜单树');
    var object = new Object();
    object.roleId = rowData.roleId;
    roleId = rowData.roleId;
    cdmk.url = path + '/system/role/getTreeData';
    cdmk.parameters = object;
    cdmk.callBack = batchAdd;
    cdmk.singleLoad = 0;
    cdmk.selectedMulti = 1;
    cdmk.init();
    cdmk.openRef();
}

function batchAdd() {
    var zTree = $.fn.zTree.getZTreeObj('cdmk');
    var checkNodes = zTree.getCheckedNodes(true);
    var menuCodes = "";
    for (var i = 0; i < checkNodes.length; i++) {
        if (menuCodes == "") {
            menuCodes = checkNodes[i].id;
        } else {
            menuCodes = menuCodes + ',' + checkNodes[i].id;
        }
    }
    var params = new Object();
    params.roleId = roleId;
    params.menuCodes = menuCodes;
    $.ajax({
        type: "POST",
        url: path + "/system/role/batchAddMenu",
        data: params,
        dataType: "json",
        success: function callback(data) {
            var analyMsg = data;
            if (analyMsg.code != "0000") {
                $.messager.alert('操作提示', analyMsg.msg, 'warning', function () {
                });
                //$.alert("add", "error", "角色信息");
                //$.alert("default", "error", analyMsg.msg);
            } else {
                $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                });
                //$.alert("add", "info", "角色信息");
            }
        }
    });
}

//启用禁用
function btnYxbzNodeByGrid() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        $.messager.alert("操作提示", "请选择一个角色信息！", "warning");
        //$.alert("pick", "info", "角色信息");
        return;
    }
    var rowData = rows[0];
    var param = new Object();
    param.roleId = rowData.roleId;
    param.validMark = rowData.validMark;
    var msg = "";
    if (param.validMark == "1") {
        msg = "停用后该角色所关联人员将无法正常使用系统，是否停用？";
    } else {
        msg = "是否启用？";
    }

    $.messager.alert("问题", msg,"confirm", function () {

            $.ajax({
                type: "POST",
                url: path + '/system/role/updateValidMark',
                data: param,
                dataType: "json",
                async: false,
                success: function callback(data) {
                    var analyMsg = data;
                    if (analyMsg.code != "0000") {
                        $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                        });
                    } else {
                        $.messager.alert('操作提示', '修改停/启成功！', 'info', function () {
                        });
                        btnQuery();
                    }
                }
            });
    });

}
